package com.googlecode.horncomb.bean.paged;

import java.util.Map;

import org.springframework.util.Assert;

public abstract class PagedUtil {

	public static int caclStartIndex(int pageSize, int page) {
		return (pageSize * (page - 1));
	}

	public static int caclEndIndexByStart(int startIndex, int pageSize,
			int count) {
		int endIndex = startIndex + pageSize;
		return (endIndex > (count - 1) ? (count - 1) : endIndex);
	}

	public static int caclEndIndexByPage(int pageSize, int page, int count) {
		int endIndex = pageSize * page;
		return (endIndex > count ? count : endIndex) - 1;
	}

	public static int caclPageSize(int startIndex, int endIndex) {
		return endIndex - startIndex + 1;
	}

	public static PagedSetting getOnlyPagedSetting(
			Map<String, PagedSetting> pagedSettingsMap) {
		Assert.isTrue(pagedSettingsMap != null && !pagedSettingsMap.isEmpty(),
				"PagedSettingsMap is empty.");
		Assert.isTrue(pagedSettingsMap.size() == 1,
				"PagedSettingsMap has more then one value.");
		return (pagedSettingsMap.values().iterator().next());
	}
}
